package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysShoppingDao;
import com.gusha.model.SysShopping;
import com.gusha.model.SysTraining;
import com.gusha.model.TrainComm;
import com.gusha.model.User;

@Service
@Transactional
public class SysShoppingService {
	@Autowired
	private SysShoppingDao sysShoppingDao;
	
	
	public Page<SysShopping> findAll(Pageable pageable, SysShopping sysShopping) {
        @SuppressWarnings("serial")
        Page<SysShopping> result = sysShoppingDao.findAll(new Specification<SysShopping>() {
            @Override
            public Predicate toPredicate(Root<SysShopping> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysShopping.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysShopping.getId()));
                }
                if (sysShopping.getCommodityType() != null) {
                    list.add(cb.equal(root.get("commodityType").as(BigInteger.class),sysShopping.getCommodityType()));
                }
                if (sysShopping.getCommodityName() != null) {
                    list.add(cb.equal(root.get("commodityName").as(String.class),sysShopping.getCommodityName()));
                }
                if (sysShopping.getCommodityImg() != null) {
                    list.add(cb.equal(root.get("commodityImg").as(String.class),sysShopping.getCommodityImg()));
                }
                if (sysShopping.getCommodityPrice() != null) {
                    list.add(cb.equal(root.get("commodityPrice").as(BigDecimal.class),sysShopping.getCommodityPrice()));
                }
                if (sysShopping.getCommodityStock() != null) {
                    list.add(cb.equal(root.get("commodityStock").as(BigInteger.class),sysShopping.getCommodityStock()));
                }
                if (sysShopping.getIsCheck() != null) {
                    list.add(cb.equal(root.get("isCheck").as(BigInteger.class),sysShopping.getIsCheck()));
                }
                if (sysShopping.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),sysShopping.getIsDelete()));
                }
                if (sysShopping.getAddTime() != null) {
                    list.add(cb.equal(root.get("addTime").as(Date.class),sysShopping.getAddTime()));
                }
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
